//
//  ZYBasicHelper.m
//  ZYhop
//
//  Created by Zyark on 2017/5/6.
//  Copyright © 2017年 Zyark. All rights reserved.
//

#import "ZYBasicHelper.h"
#import "NSErrorHelper.h"
#import "ZYLoginMainVC.h"
#import "AppDelegate.h"
#import "RHSocketService.h"

@implementation ZYBasicHelper
+ (NSString *)stringWithTimelineDate:(NSDate *)date {
    if (!date) return @"";
    
    static NSDateFormatter *formatterYesterday;
    static NSDateFormatter *formatterSameYear;
    static NSDateFormatter *formatterFullDate;
    
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        formatterYesterday = [[NSDateFormatter alloc] init];
        [formatterYesterday setDateFormat:@"昨天 HH:mm"];
        [formatterYesterday setLocale:[NSLocale currentLocale]];
        
        formatterSameYear = [[NSDateFormatter alloc] init];
        [formatterSameYear setDateFormat:@"M-d"];
        [formatterSameYear setLocale:[NSLocale currentLocale]];
        
        formatterFullDate = [[NSDateFormatter alloc] init];
        [formatterFullDate setDateFormat:@"yy-M-dd"];
        [formatterFullDate setLocale:[NSLocale currentLocale]];
    });
    
    NSDate *now = [NSDate new];
    NSTimeInterval delta = now.timeIntervalSince1970 - date.timeIntervalSince1970;
    if (delta < -60 * 10) { // 本地时间有问题
        return [formatterFullDate stringFromDate:date];
    } else if (delta < 60 * 10) { // 10分钟内
        return @"刚刚";
    } else if (delta < 60 * 60) { // 1小时内
        return [NSString stringWithFormat:@"%d分钟前", (int)(delta / 60.0)];
    } else if (date.isToday) {
        return [NSString stringWithFormat:@"%d小时前", (int)(delta / 60.0 / 60.0)];
    } else if (date.isYesterday) {
        return [formatterYesterday stringFromDate:date];
    } else if (date.year == now.year) {
        return [formatterSameYear stringFromDate:date];
    } else {
        return [formatterFullDate stringFromDate:date];
    }
}

+ (BOOL)validateMobile:(NSString *)mobile
{
    // 130-139  150-153,155-159  180-189  145,147  170,171,173,176,177,178
    NSString *phoneRegex = @"^[1][0-9]{10}$";
    NSPredicate *phoneTest = [NSPredicate predicateWithFormat:@"SELF MATCHES %@",phoneRegex];
    return [phoneTest evaluateWithObject:mobile];
}

+ (BOOL)validateEmail:(NSString *)email
{
    // 130-139  150-153,155-159  180-189  145,147  170,171,173,176,177,178
    NSString *emailRegex = @"[A-Z0-9a-z._%+-]+@[A-Za-z0-9.-]+\\.[A-Za-z]{2,4}";
    NSPredicate *emailTest = [NSPredicate predicateWithFormat:@"SELF MATCHES %@",emailRegex];
    return [emailTest evaluateWithObject:email];
}

+ (BOOL)validateQQ:(NSString *)qq{
    NSString *qqRegex = @"[1-9][0-9]{4,14}";
    NSPredicate *qqTest = [NSPredicate predicateWithFormat:@"SELF MATCHES %@",qqRegex];
    return [qqTest evaluateWithObject:qq];
}

/**
 处理网络请求错误

 @param error 错误
 @param message 错误显示的消息，如果为空则显示服务器返回的错误信息
 @param controller 跳转的根Controller
 */
+(void)handleErrorMessaeWithError:(NSError *)error message:(NSString *)message controller:(UIViewController *)controller{
    NSString *errorInfo = [NSErrorHelper handleErrorMessage:error];
    if([errorInfo isEqualToString:LOGIN_FAILURE]){
        //登录失效
        [ZYConfig clearUserInfo];
        [[ToastHelper sharedToastHelper] toastError:[[ZYChangeLanguage sharedZYChangeLanguage] getStringByKey:LC_ERROR_LOGIN_INVALID]];
        [ZYBasicHelper goToLogin:controller];
    }else if(message){
        [[ToastHelper sharedToastHelper] toastError:message];
    }else{
        [[ToastHelper sharedToastHelper] toastError:errorInfo];
    }
}

//跳转到登录控制器
+(void)goToLogin:(UIViewController *)controller{
    if([[RHSocketService sharedInstance] isRunning]){
        [[RHSocketService sharedInstance] stopService];
    }
    ZYLoginMainVC *vc = [[ZYLoginMainVC alloc] init];
    ZYNavigationController *nav = [[ZYNavigationController alloc] initWithRootViewController:vc];
    AppDelegate * appDelegate = (AppDelegate*)[UIApplication sharedApplication].delegate;
    appDelegate.window.rootViewController = nav;
}

@end
